<template>
  <!-- 新增设计主管页面 -->
  <div v-loading="loading">
    <el-form :inline="true" ref="ruleFormRef" :model="form" label-width="120px">
      <el-form-item label="下单内容:">
        <el-input disabled v-model="comment" type="textarea" style="width: 400px" />
      </el-form-item>
      <el-form-item label="备注:">
        <el-input  v-model="form.comment" type="textarea" style="width: 400px" />
      </el-form-item>
    </el-form>
    <div class="_button">
      <el-button type="primary" size="large" @click="onSubmit(ruleFormRef)">保存</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineEmits, watch } from 'vue';
  import { tbOrderStatusListData } from '/@/api/tb/tbOrderStatus';
  import {tbsupplierSplitOrder,tbmanufactureToDelivery } from '/@/api/tb/tbaddNext';
  import { OrderListData} from '/@/api/tb/tbOrder';
  import { ElMessage } from 'element-plus';
  const ruleFormRef = ref<any>();
  const loading = ref(true);
  const emit = defineEmits(['editForm']);
  const form = reactive({
    orderId: '', // 订单号
    executor: '', //订单管理员
    orderStatus: '', // 订单状态
    nextorderStatus: '', //下一步订单状态
    reserveAmount: '', // 定金金额
    comment: '', // 备注
    designer: '', // 设计师
    officeCode: '', // 设计部门
    estimatedAmount: '', //预估金额
    fileList: '',
    money: '',
    measuringTime: '', //量尺时间
  });
  const comment=ref('')
  const props = defineProps({
    orderid: String,
  });
  watch(
    () => props.orderid, //这样才可以检测到对象里面的值
    async () => {
      const res = await OrderListData({ orderId: props.orderid });
      if (res.list) {
        for (let key in form) {
          form[key] = '';
          if (key != 'comment') {
            form[key] = res.list[0][key];
          }
        }
      }
      const getComment = await tbOrderStatusListData({
        orderId: props.orderid,
        orderStatus: res.list[0]?.orderStatus,
        finishStatus: '1',
      });
      if (getComment.list) {
        loading.value=false
        comment.value=getComment.list[0].comment
      }
      console.log(form.orderStatus);
      
    },
    { immediate: true },
  );

  const onSubmit = async (formEl) => {
    if (!formEl) return;
    await formEl.validate(async (valid) => {
      if (valid) {
        const res=ref()
        if(form.orderStatus=='生产'){
         res.value = await tbmanufactureToDelivery(form);
       

        }else{
          res.value = await tbsupplierSplitOrder(form);
        }
        
        if (JSON.parse(JSON.stringify(res.value)).result) {
          for (let key in form) {
            form[key] = '';
          }
          form.orderType = '1';
          form.orderStatus = '4';
          emit('editForm');
          ElMessage({
            message: JSON.parse(JSON.stringify(res.value)).message,
            type: 'success',
          });
        } else {
          ElMessage.error(JSON.parse(JSON.stringify(res.value)).message);
        }
      }
    });
  };
</script>
<style lang="less" scoped>
  ._button {
    width: 100%;
    display: flex;
    justify-content: flex-end;
    padding-right: 50px;
  }
  .el-input {
    width: 300px;
  }
</style>
